package com.wnx.job.modules.jms.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.common.mapstruct.StudentMapper;
import com.wnx.job.modules.jms.dto.StudentParam;
import com.wnx.job.modules.jms.mapper.JmsStudentMapper;
import com.wnx.job.modules.jms.model.JmsProfession;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsProfessionService;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.model.PmsResume;
import com.wnx.job.modules.pms.service.PmsResumeService;
import com.wnx.job.modules.ums.dto.UmsAdminParam;
import com.wnx.job.modules.ums.model.UmsAdmin;
import com.wnx.job.modules.ums.service.UmsAdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 学生 服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class JmsStudentServiceImpl extends ServiceImpl<JmsStudentMapper, JmsStudent> implements JmsStudentService {

    @Resource
    private JmsStudentMapper studentMapper;
    @Resource
    private JmsProfessionService professionService;
    @Resource
    private PmsResumeService resumeService;
    @Resource
    private UmsAdminService adminService;

    /**
     * 根据账号查询学生信息
     * @param account
     * @return
     */
    @Override
    public JmsStudent findByAccount(String account) {
        return studentMapper.selectOne(new QueryWrapper<JmsStudent>().eq("account",account));
    }
    /**
     * 根据ID查询学生信息
     * @param id
     * @return
     */
    @Override
    public StudentParam findById(Long id) {
        return studentMapper.findById(id);
    }

    /**
     * 批量保存学生信息
     * @param studentParamList
     */
    @Override
    public void addBatch(List<StudentParam> studentParamList) {
        studentParamList.forEach(this::saveStudent);
    }

    /**
     * 新增学生信息
     * @param studentParam
     */
    @Override
    public void saveStudent(StudentParam studentParam) {
        //检查上传文件数据完整性
        if (
                StringUtils.isNotBlank(studentParam.getName()) &&
                        StringUtils.isNotBlank(studentParam.getAccount()) &&
                        StringUtils.isNotBlank(studentParam.getPhone()) &&
                        studentParam.getBirthday() != null &&
                        StringUtils.isNotBlank(studentParam.getCollege()) &&
                        StringUtils.isNotBlank(studentParam.getEducation()) &&
                        studentParam.getSex() != null &&
                        StringUtils.isNotBlank(studentParam.getEmail())) {
            //校验学号是否已经被使用
            String account = studentParam.getAccount();
            JmsStudent tbStudentByDb = this.findByAccount(account);
            if (tbStudentByDb != null) {
                Asserts.fail("该学号已经被使用了，请更换。");
            }

            //调用持久层，新增学生
            JmsStudent tbStudent = StudentMapper.INSTANCE.toPojo(studentParam);
            if (StringUtils.isNotBlank(studentParam.getProfession())) {
                JmsProfession tbProfession = professionService.findByName(studentParam.getProfession());
                tbStudent.setProfessionId(tbProfession.getId());
            }

            tbStudent.setCreated(new Date());
            tbStudent.setStatus(Const.STATUS_ON);
            tbStudent.setPhoto(Const.DEFAULT_STUDENT_PHOTO);
            tbStudent.setPolitical(Const.DEFAULT_STUDENT_POLITICAL);
            tbStudent.setDescription(Const.DEFAULT_STUDENT_DESCRIPTION);
            studentMapper.insert(tbStudent);

            //调用持久层，新增该学生简历
            PmsResume resume = new PmsResume();
            resume.setStudentId(tbStudent.getId());
            resume.setJobStatus(Const.DEFAULT_PASSWORD);
            resume.setRemark(Const.DEFAULT_REMARK);
            resume.setJobStatus(Const.DEFAULT_JOB_STATUS);
            resumeService.save(resume);

            //调用持久层，添加用户信息。
            UmsAdminParam admin = new UmsAdminParam();
            admin.setUsername(tbStudent.getAccount());
            admin.setPassword(Const.DEFAULT_PASSWORD);
            admin.setIcon(Const.DEFAULT_AVATAR);
            admin.setNickName(studentParam.getName());
            admin.setEmail(tbStudent.getEmail());
            admin.setNote(Const.DEFAULT_NOTE_STUDENT);
            UmsAdmin register = adminService.register(admin);
            //分配用户，学生角色
            adminService.updateRole(register.getId(), Collections.singletonList(Const.ROLE_STUDENT));
        }
    }


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<StudentParam> findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<StudentParam> studentList =  studentMapper.findByCondition(queryPageBean.getQueryParams());
        return PageResult.restPage(studentList);
    }

    /**
     * 删除学生信息
     * @param id
     */
    @Override
    public void deleteStudent(Long id) {
        JmsStudent jmsStudent = studentMapper.selectById(id);
        jmsStudent.setStatus(Const.STATUS_OFF);
        studentMapper.updateById(jmsStudent);

    }
}
